import os
import sys
from concurrent.futures import ProcessPoolExecutor, as_completed

sys.path.insert(0, "./python")
from common import AuthInfo, BaseMachineInfo, BaseDeploy


class Release(BaseDeploy):
    def main(self):
        if self.host["status"] != "Ready":
            self.op_release()
        return True


def deploy_one(host, maas_url, api_key):
    auth = AuthInfo(maas_url, api_key)
    deployer = Release(maas_url, auth.session, host)
    deployer.main()
    return host["Hostname"]


if __name__ == '__main__':
    maas_url = os.getenv("maas_url")
    api_key = os.getenv("api_key")
    hosts = sys.argv[1].split("=", 1)[1]

    auth = AuthInfo(maas_url, api_key)
    get_machine_info = BaseMachineInfo(auth.session, maas_url, hosts)

    with ProcessPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(deploy_one, host, maas_url, api_key) for host in get_machine_info.results]
        for f in as_completed(futures):
            print(f"release完成: {f.result()}")
